package com.cmc.printApplet.receiver;

import com.alibaba.fastjson.JSON;
import com.cmc.printApplet.CirculateOrder.DrawCirculateOrder;
import com.cmc.printApplet.service.PrintCirculateOrderService;
import com.cmc.printApplet.util.PageFormatSub;
import com.cmc.printApplet.vo.CirculateOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.awt.print.Book;
import java.awt.print.Paper;

/**
 * 包装流转单接收方法
 * 注意queueName 一定要和T_SYSTEM_ORG表orgId保持一致
 */
@Component
@RabbitListener(queues = "${spring.profiles.order}")
public class OrderMqReceiver {

    private Logger logger = LoggerFactory.getLogger(OrderMqReceiver.class);

    @Value("${spring.profiles.orgId}")
    private String orgIds;

    @RabbitHandler
    public void process(String msg){
        logger.debug("接收到到对象信息为"+msg);
        CirculateOrder order = JSON.parseObject(msg,CirculateOrder.class);
        logger.info("接收到包装流转单打印信息，开始判断仓库Id是否正确"+order.getOrgId());
        logger.debug("打印人Id:"+order.getPeopleId()+";打印人姓名:"+order.getPeopleName()+";打印时间:"+order.getPrintTime()+";答应仓库名称:"+order.getOrgName());
        for (String orgId:orgIds.split(";")){
            if (orgId.equals(order.getOrgId())){
                logger.info("打印信息为："+msg);
                DrawCirculateOrder pageFirst = new DrawCirculateOrder(order);
                PrintCirculateOrderService printService = new PrintCirculateOrderService();
                logger.info("设置纸张大小");
                Paper p = new Paper();
                p.setSize(pageFirst.WIDTH_NUM, pageFirst.HEIGTH_NUM);// 纸张大小
                p.setImageableArea(0, 0, pageFirst.WIDTH_NUM, pageFirst.HEIGTH_NUM);

                PageFormatSub pf = new PageFormatSub();
                pf.setOrientation(PageFormatSub.PORTRAIT);
                pf.setPaper(p);
                logger.info("生成打印对象");
                Book book = new Book();
                book.append(printService, pf);
                logger.info("开始调用打印程序");
                printService.dealPrint(pageFirst, book,order);
            }
        }
    }
}
